# superclass of attachments
class Attachment < ActiveRecord::Base
  has_many :attachment_links
  belongs_to :user

  def inspect
    "Attachment"
  end
end

# an attachment that references a comment in FM
class CommentRef < Attachment
  validates_presence_of :ref_id
  validates_numericality_of :ref_id

  def comment
    @comment ||= Comment.find( ref_id )
  end

  def inspect
    "CommentRef( ##{ref_id} )"
  end
end

# an attachment that references an issue in FM
class IssueRef < Attachment
  validates_presence_of :ref_id
  validates_numericality_of :ref_id

  def issue
    @issue ||= Issue.find( ref_id )
  end

  def project
    issue.project
  end

  def local_id
    issue.local_id
  end

  def inspect
    "IssueRef( #{project.name} ##{local_id} )"
  end
end

# an image attachment - resizes, thumbnails attachment
class Image < Attachment
  has_attachment :content_type => :image,
                  :storage => :file_system,
                  :max_size => MAX_ATTACHMENT_SIZE,
                  :resize_to => RESIZE_GEO_STRING,
                  :thumbnails => { :thumb => THUMB_GEO_STRING }

  validates_as_attachment

  def inspect
    "Image( #{filename} )"
  end
end

# a patch file attachment - special hander for patch files
class PatchFile < Attachment
  has_attachment :storage => :file_system,
                 :max_size => MAX_ATTACHMENT_SIZE

  validates_as_attachment
  validates_presence_of :ref_id
  validates_numericality_of :ref_id

  after_create :notify

  def notify(*args)
  end

  def inspect
    "PatchFile( #{filename} )"
  end
end

# any file attachment - no special handling
class GenericFile < Attachment
  has_attachment :storage => :file_system,
                  :max_size => MAX_ATTACHMENT_SIZE

  validates_as_attachment

  def inspect
    "File( #{filename} )"
  end
end
